package cn.zqh.brand.service.impl;

import cn.zqh.bean.api.CommonResult;
import cn.zqh.bean.api.ResultCode;
import cn.zqh.bean.bo.Brand;
import cn.zqh.bean.bo.CreateABrandParams;
import cn.zqh.brand.config.BSException;
import cn.zqh.brand.mapper.BrandMapper;
import cn.zqh.brand.service.BrandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zqh
 * @since 2023-01-12
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Resource
    private BrandMapper brandMapper;
    
    /**
      * @Author: zqh
      * @Date: 
      * @Description: 创建某个品牌
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public CommonResult CreateANewBrand(CreateABrandParams createABrandParams) {
        Brand brand = brandMapper.selectOne(new QueryWrapper<Brand>().eq("Brand_name", createABrandParams.getBrandName()));
        if (brand != null) {
            throw new BSException(ResultCode.THEBRANDALREADYEXISTS.getCode(),ResultCode.THEBRANDALREADYEXISTS.getMessage());
        }
        Brand brand1 = new Brand();
        brand1.setBrandName(createABrandParams.getBrandName())
                .setIsDelete(0)
                .setBrandAvatar(createABrandParams.getBrandAvatar());
        int insert = brandMapper.insert(brand1);
        if (insert != 1){
            throw new BSException(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
        }
        return CommonResult.success(ResultCode.SUCCESS);
    }
    
    /**
      * @Author: zqh
      * @Date: 
      * @Description: 删除某个品牌
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public CommonResult DelBrandById(Integer id) {
        int i = brandMapper.delete(new QueryWrapper<Brand>().eq("Brand_id", id));
        if (i != 1){
            throw new BSException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMessage());
        }
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过id查询某个品牌
    */
    @Override
    @Transactional(readOnly = true)
    public CommonResult getBrandById(Integer id) {
        Brand brand = brandMapper.selectOne(new QueryWrapper<Brand>().eq("Brand_id", id));
        if (brand == null) {
            throw new BSException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMessage());
        }
        return CommonResult.success(brand);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改品牌信息
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public CommonResult UpdateBrandMation(Integer id,String avatar,String brandName) {
        Brand brand = brandMapper.selectById(id);
        if (brand == null){
            throw new BSException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMessage());
        }
        brand.setBrandAvatar(avatar)
                .setBrandName(brandName);
        int i = brandMapper.updateById(brand);
        if (i != 1){
            throw new BSException(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMessage());
        }

        return CommonResult.success(ResultCode.SUCCESS);
    }
     /**
       * @Author: zqh
       * @Date:
       * @Description: 获取所有品牌
     */
    @Override
    @Transactional(readOnly = true)
    public CommonResult getAllBrand(Integer page, Integer size) {
        List<Brand> brands = brandMapper.getAllBrandMap((page-1)*size,size);
        return CommonResult.success(brands);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 查询被删除了的品牌
    */
    @Override
    @Transactional(readOnly = true)
    public CommonResult getDelBrand(Integer page, Integer size) {
        page = (page - 1) * size;
        List<Brand> brands = brandMapper.getDelBrandMap(page,size);
        return CommonResult.success(brands);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 把被删除了的品牌取消删除
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public CommonResult cancelBrand(Integer id) {
        brandMapper.updateIsDel(id);
        return CommonResult.success(ResultCode.SUCCESS);
    }
    
    /**
      * @Author: zqh
      * @Date: 
      * @Description: 查询所有品牌的数量
    */
    @Override
    @Transactional(readOnly = true)
    public CommonResult getBrandNumber() {
        Integer number = brandMapper.getAllBrandNumber();
        return CommonResult.success(number);
    }
}
